#include "mainwidget.h"
#include "./ui_mainwidget.h"

#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QAction>
#include "showinfowindow.h"
#include "messageeditarea.h"
#include "messageshowarea.h"
#include "sessiondetailwidget.h"
#include "debug.h"

MainWidget* MainWidget::instance = nullptr; //初始化单例指针

MainWidget *MainWidget::getInstance()
{
    if(instance ==nullptr)
    {
        instance = new MainWidget();
    }
    return instance;
}

MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainWidget)
{
    ui->setupUi(this);
    //初始化主界面
    mainWindowInit();
    //左侧布局
    leftWindowInit();
    //中间布局
    midWindowInit();
    //右侧布局
    rightWindowInit();
    //信号槽连接
    establishConnection();
}

void MainWidget::mainWindowInit()
{
    this->setWindowIcon(QIcon(":/resource/images/logo.png"));
    this->setWindowTitle("个人微信");
    QHBoxLayout* layout = new QHBoxLayout();

    //初始化界面
    leftWindow = new QWidget();
    midWindow = new QWidget();
    rightWindow = new QWidget();

    //设置参数
    leftWindow->setFixedWidth(60);
    leftWindow->setStyleSheet("QWidget {background-color: rgb(45, 45, 45)}");

    midWindow->setFixedWidth(270);
    midWindow->setStyleSheet("QWidget {background-color: #f7f7f7");

    rightWindow->setMinimumWidth(650);
    rightWindow->setStyleSheet("QWidget {background-color: rgb(245, 245, 245)}");

    //边距设置
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);

    //添加到布局管理器中
    layout->addWidget(leftWindow);
    layout->addWidget(midWindow);
    layout->addWidget(rightWindow);
    this->setLayout(layout);
}

void MainWidget::leftWindowInit()
{
    QVBoxLayout* layout = new QVBoxLayout();
    leftWindow->setLayout(layout);
    //设置参数
    layout->setContentsMargins(0, 40, 0, 0);
    layout->setSpacing(15); //设置间隔

    QString StyleSheet = "QPushButton {"
                         "    background-color: transparent;"
                         "    border: none;"
                         "    color: black;"  // 默认文字颜色
                         "}";
    avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(QSize(45, 45));
    avatarBtn->setIcon(QIcon(":/resource/images/avatar.jpg"));
    avatarBtn->setIconSize(QSize(30, 30));
    avatarBtn->setStyleSheet(StyleSheet);

    sessionTabBtn = new QPushButton();
    sessionTabBtn->setFixedSize(QSize(45, 45));
    sessionTabBtn->setIcon(QIcon(":/resource/images/session_active.png"));
    sessionTabBtn->setIconSize(QSize(30, 30));
    sessionTabBtn->setStyleSheet(StyleSheet);

    friendTabBtn = new QPushButton();
    friendTabBtn->setFixedSize(QSize(45, 45));
    friendTabBtn->setIcon(QIcon(":/resource/images/friend_inactive.png"));
    friendTabBtn->setIconSize(QSize(30, 30));
    friendTabBtn->setStyleSheet(StyleSheet);

    groupChatTabBtn = new QPushButton();
    groupChatTabBtn->setFixedSize(QSize(45, 45));
    groupChatTabBtn->setIcon(QIcon(":/resource/images/apply_inactive.png"));
    groupChatTabBtn->setIconSize(QSize(30, 30));
    groupChatTabBtn->setStyleSheet(StyleSheet);

    //添加按钮
    layout->addWidget(avatarBtn, 1, Qt::AlignTop | Qt::AlignHCenter);
    layout->addWidget(sessionTabBtn, 1, Qt::AlignTop | Qt::AlignHCenter);
    layout->addWidget(friendTabBtn, 1, Qt::AlignTop | Qt::AlignHCenter);
    layout->addWidget(groupChatTabBtn, 1, Qt::AlignTop | Qt::AlignHCenter);

    //下⽅添加空隙, 把上述控件挤上去.
    layout->addStretch(20);

}

void MainWidget::midWindowInit()
{
    //设置网格布局
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(0, 20, 0, 0);
    layout->setSpacing(0);
    midWindow->setLayout(layout);

    search = new QLineEdit();
    search->setStyleSheet(
        "QLineEdit {"
        "   padding-left: 15px;"
        "   background-position: left center;"
        "   background-repeat: no-repeat;"
        "   border: none;"
        "   border-radius: 5px;"
        "   background-color: rgb(226, 226, 226);"
        "}"
        );
    search->setPlaceholderText("搜索");
    search->setFixedHeight(30);

    addUserBtn = new QPushButton();
    addUserBtn->setIcon(QIcon(":/resource/images/cross.png"));
    addUserBtn->setIconSize(QSize(17, 17));
    addUserBtn->setStyleSheet("QPushButton { background-color: rgb(226, 226, 226)}");
    addUserBtn->setFixedSize(QSize(30, 30));

    sessionFriendArea = new SessionFriendArea();

    QWidget* spacer1 = new QWidget();
    spacer1->setFixedWidth(10);
    QWidget* spacer2 = new QWidget();
    spacer2->setFixedWidth(10);
    QWidget* spacer3 = new QWidget();
    spacer3->setFixedWidth(10);

    layout->addWidget(spacer1, 0, 0);
    layout->addWidget(search, 0, 1);
    layout->addWidget(spacer2, 0, 2);
    layout->addWidget(addUserBtn, 0, 3);
    layout->addWidget(spacer3, 0, 4);
    layout->addWidget(sessionFriendArea, 1, 0, 1, 5);
}

void MainWidget::rightWindowInit()
{
    QVBoxLayout* layout = new QVBoxLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setAlignment(Qt::AlignTop);
    rightWindow->setLayout(layout);

    //最上面的标题和扩展选项
    QWidget *topTitle = new QWidget();
    //给widget设置边框
    topTitle->setObjectName("topTitle");
    topTitle->setStyleSheet("#topTitle {border-bottom: 1px solid rgb(230, 230, 230); border-left:1px solid rgb(230, 230, 230);}");
    topTitle->setFixedHeight(62); // 固定高度
    topTitle->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); //水平可以扩展， 垂直不可以扩展
    QHBoxLayout* titleLayout = new QHBoxLayout();
    titleLayout->setSpacing(0);
    titleLayout->setContentsMargins(10,0,10,0);
    topTitle->setLayout(titleLayout);

    layout->addWidget(topTitle);

    //添加标题和...
    QLabel* sessionTitle = new QLabel();
    sessionTitle->setStyleSheet("QLabel {font-size: 20px; border-bottom: 1px solid rgb(230, 230, 230)}");
#if TEST_UI
    sessionTitle->setText("这是绘画的标题");
#endif
    titleLayout->addWidget(sessionTitle);

    extendBtn = new QPushButton();
    extendBtn->setFixedSize(QSize(40, 40));
    extendBtn->setIcon(QIcon(":/resource/images/more.png"));
    extendBtn->setIconSize(QSize(30, 30));
    extendBtn->setStyleSheet("QPushButton {border: none; background-color: rgb(245, 245, 245);} "
                             " QPushButton:pressed{background-color: rgb(230, 230, 230)}");
    titleLayout->addWidget(extendBtn);


    //消息显示区域
    MessageShowArea* showArea = new MessageShowArea();
    layout->addWidget(showArea);
    //消息编辑区
    MessageEditArea* editArea = new MessageEditArea();
    layout->addWidget(editArea);
}

//链接信号槽
void MainWidget::establishConnection()
{
    //左侧界面按钮
    connect(sessionTabBtn, &QPushButton::clicked, this, &MainWidget::sessionChange);
    connect(friendTabBtn, &QPushButton::clicked, this, &MainWidget::friendChange);
    connect(groupChatTabBtn, &QPushButton::clicked, this, &MainWidget::groupChatChange);

    //点击头像显示个人信息
    connect(avatarBtn, &QPushButton::clicked, this, [this](){
        showinfoWindow* infoWindow = new showinfoWindow(this);
        infoWindow->exec(); //模态
    });

    //...关联
    connect(extendBtn, &QPushButton::clicked, this, [this](){
        sessionDetailWidget* sdw = new sessionDetailWidget(this);
        sdw->exec();
    });

}

void MainWidget::sessionChange()
{
    active = SESSION_TAB;
    sessionTabBtn->setIcon(QIcon(":/resource/images/session_active.png"));
    friendTabBtn->setIcon(QIcon(":/resource/images/friend_inactive.png"));
    groupChatTabBtn->setIcon(QIcon(":/resource/images/apply_inactive.png"));
    loadSessionList();
}

void MainWidget::friendChange()
{
    active = FRIEND_TAB;
    sessionTabBtn->setIcon(QIcon(":/resource/images/session_inactive.png"));
    friendTabBtn->setIcon(QIcon(":/resource/images/friend_active.png"));
    groupChatTabBtn->setIcon(QIcon(":/resource/images/apply_inactive.png"));
    loadFriendList();
}

void MainWidget::groupChatChange()
{
    active = GROUPCHAT_TAB;
    sessionTabBtn->setIcon(QIcon(":/resource/images/session_inactive.png"));
    friendTabBtn->setIcon(QIcon(":/resource/images/friend_inactive.png"));
    groupChatTabBtn->setIcon(QIcon(":/resource/images/apply_active.png"));
    loadGrioupChatList();
}

void MainWidget::loadSessionList()
{

}

void MainWidget::loadFriendList()
{

}

void MainWidget::loadGrioupChatList()
{

}

MainWidget::~MainWidget()
{
    delete ui;
}

//获取单例

